//
//  AboutViewController.m
//  JinMaJiaDemo
//
//  Created by GripMeMory on 15/12/29.
//  Copyright © 2015年 GripMeMory. All rights reserved.
//

#import "AboutViewController.h"
#import "TableViewDataSource.h"
#import "AboutViewCell.h"
#import "AboutModel.h"

#define kNavigationBarHeight ([[[UIDevice currentDevice] systemVersion] intValue] >= 7.0 ? 64 : 44)

#define kTitleViewTopMargin 75 / 2
#define kTitleViewHeight 350 / 2
#define kLogoImageViewWidth 170 / 2
#define kLogoImageViewTopMargin 45 / 2
#define kLogoInfoLabelTopMargin 30 / 2
#define kLogoInfoLabelHeight 30 / 2

#define kBreakRedLineTopMargin 65 / 2

static NSString * const cellName = @"AboutCellIdentifier";

@interface AboutViewController () <UITableViewDelegate>

@property (nonatomic, strong) TableViewDataSource *dataSource;

@end

@implementation AboutViewController

{
    UIView *_titleView;
    UIView *_seperateLine;
    UITableView *_tableView;
    
    NSMutableArray *_list;
}

#pragma mark - View Controller Life Cycle

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController popToRootViewControllerAnimated:NO];
    self.navigationController.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self fakeValue];
    [self customNav];
    [self customUI];
    [self customSeperateLine];
    [self setupTableView];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Fake Value

- (void)fakeValue{
    AboutModel *model1 = [[AboutModel alloc] init];
    AboutModel *model2 = [[AboutModel alloc] init];
    model1.title = @"客服电话:";
    model1.detail = @"4006-010-661";
    model2.title = @"微信服务号:";
    model2.detail = @"jinmajiaweixin";
    _list = [[NSMutableArray alloc] initWithArray:@[model1, model2]];
}

#pragma mark - Custom NavigationBar

- (void)customNav{
    UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"返回"] style:UIBarButtonItemStylePlain target:self action:@selector(backBarButtonItemClick:)];
    self.navigationItem.leftBarButtonItem = bbi;
    
    self.title = @"关于我们";
}

#pragma mark - Custom User Interface

- (void)customUI{
    self.view.backgroundColor = RGB_COLOR(@"#f6f6f6");
    
    _titleView = [[UIView alloc] initWithFrame:CGRectMake(0, kNavigationBarHeight + kTitleViewTopMargin, self.view.width, kTitleViewHeight)];
    _titleView.backgroundColor = [UIColor whiteColor];
    
    UIImageView *titleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.centerX - kLogoImageViewWidth / 2, kLogoImageViewTopMargin, kLogoImageViewWidth, kLogoImageViewWidth)];
    titleImageView.image = [UIImage imageNamed:@"金马甲logo"];
    [_titleView addSubview:titleImageView];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, titleImageView.bottom + kLogoInfoLabelTopMargin, _titleView.width, kLogoInfoLabelHeight)];
    label.textAlignment = NSTextAlignmentCenter;
    label.text = [NSString stringWithFormat:@"金马甲：v %@", [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]];
    [_titleView addSubview:label];
    
    [self.view addSubview:_titleView];
    
}

- (void)customSeperateLine{
    _seperateLine = [[UIView alloc] initWithFrame:CGRectMake(0, _titleView.bottom + kBreakRedLineTopMargin, self.view.width, 3)];
    _seperateLine.backgroundColor = [UIColor redColor];
    [self.view addSubview:_seperateLine];
}

- (void)setupTableView{
    TableViewCellConfigurBlock cellConfigureBlock = ^(AboutViewCell *cell, AboutModel *model){
        [cell configureCellWithModel:model];
    };
    _dataSource = [[TableViewDataSource alloc] initWithItems:_list cellIdentifier:cellName configureBlock:cellConfigureBlock];
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, _seperateLine.bottom, self.view.width, 100) style:UITableViewStylePlain];
    [_tableView registerClass:[AboutViewCell class] forCellReuseIdentifier:cellName];
    _tableView.dataSource = _dataSource;
    _tableView.delegate = self;
    _tableView.scrollEnabled = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    [_tableView reloadData];
}

#pragma mark - TableView Delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50.0f;
}

#pragma mark - Bar Item Action

- (void)backBarButtonItemClick:(UIBarButtonItem *)bbi{
    [self.navigationController popToRootViewControllerAnimated:NO];
}

@end
